package com.online.edu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.online.edu.course.entity.ChapterPeriod;
import com.online.edu.course.entity.File;
import com.online.edu.course.entity.Vo.PeriodVo;
import com.online.edu.course.service.IChapterPeriodService;
import com.online.edu.course.service.IChapterService;
import com.online.edu.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课时表 前端控制器
 * </p>
 *
 * @author world
 * @since 2021-07-30
 */
@Api(tags = "课时表接口")
@RestController
@RequestMapping("/course/chapter-period")
public class ChapterPeriodController {

    @Autowired
    IChapterPeriodService chapterPeriodService;

    @Autowired
    IChapterService chapterService;

    //课时视频文件上传
    @ApiOperation("课时视频文件上传")
    @PostMapping("/uploadMovie")
    public Result uploadMovie(MultipartFile multipartFile){
        File file = chapterPeriodService.saveFile(multipartFile);
        if (file.getId() > 0){
            return Result.success("上传成功！",file.getId());
        }
        else return Result.error("上传失败！");
    }

    //课时文件上传
    @ApiOperation("课时文件上传")
    @PostMapping("/uploadFile")
    public Result uploadFile(MultipartFile multipartFile){
        File file = chapterPeriodService.saveFile(multipartFile);
        if (file.getId() > 0){
            return Result.success("上传成功！",file.getId());
        }
        else return Result.error("上传失败！");
    }

    //获取课时表
    @ApiOperation("更具章节id获取课时表")
    @GetMapping("/list/{chapterId}")
    public Result<List<ChapterPeriod>> list(@PathVariable("chapterId")long chapterId){
        QueryWrapper<ChapterPeriod> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<ChapterPeriod> chapterPeriodList = chapterPeriodService.list(queryWrapper);
        return Result.ok(chapterPeriodList);
    }

/*    //编辑课时(根据id查询课时)
    @ApiOperation(("编辑课时(根据id查询课时)"))
    @GetMapping("/update")
    public Result<ChapterPeriod> update(@RequestParam("id")long id){
        ChapterPeriod chapterPeriod = chapterPeriodService.getById(id);
        return Result.ok(chapterPeriod);
    }*/

    //判断新增or更新
    @ApiOperation("判断新增or更新")
    @PostMapping("/save")
    public Result save(@RequestBody PeriodVo periodVo){
        ChapterPeriod chapterPeriod = periodVo.getChapterPeriod();
        long movieId = periodVo.getMovieID();
        long fileId = periodVo.getFileId();
        long chapterId = periodVo.getChapterId();
        if (chapterPeriod.getId() != null){
            boolean b = chapterPeriodService.updateById(chapterPeriod);
            if (b){
                return Result.success("更新成功！");
            }
            else {
                return Result.error("更新失败！");
            }
        }
        else {
            //新增课时表
            ChapterPeriod addChapterPeriod = chapterPeriodService.addChapterPeriod(chapterPeriod, chapterId);
            //新增课时和文件的关联表
            if (movieId > 0){
                chapterPeriodService.addPeriodFileMapping(addChapterPeriod.getId(),movieId);
            }
            if (fileId > 0){
                chapterPeriodService.addPeriodFileMapping(addChapterPeriod.getId(),fileId);
            }
            return Result.success("新增成功！");
        }
    }

    //逻辑删除
    @ApiOperation("逻辑删除")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") long id){
        ChapterPeriod chapterPeriod = chapterPeriodService.getById(id);
        UpdateWrapper<ChapterPeriod> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("logical_state",1);
        boolean b = chapterPeriodService.update(chapterPeriod, updateWrapper);
        if (b) {
            return Result.success("删除成功！");
        }
        else {
            return Result.error("删除失败！");
        }
    }
}
